Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise

Package Overview
Dependencies
Maintainers
3
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise

Bare bones Promises/A+ implementation

  • 8.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16M
increased by4.93%
Maintainers
3
Weekly downloads
 
Created

What is promise?

The 'promise' npm package provides a robust library for working with promises in JavaScript. It allows for the creation, manipulation, and composition of promises for asynchronous operations. It offers features such as deferreds, helpers for common patterns, and methods for controlling the flow of promise chains.

What are promise's main functionalities?

Creating a new Promise

This feature allows you to create a new promise. The constructor takes a function that contains the asynchronous operation. The function provides two arguments, resolve and reject, which are used to settle the promise.

var Promise = require('promise');

var promise = new Promise(function (resolve, reject) {
  // Asynchronous operation here
  if (/* operation successful */) {
    resolve('Success!');
  } else {
    reject(Error('Failure.'));
  }
});

Promise Chaining

Promise chaining enables you to link together multiple asynchronous operations. Each .then() returns a new promise, allowing for further chaining. The .catch() method is used for error handling.

promise.then(function(result) {
  console.log(result); // 'Success!'
  return 'Another value';
}).then(function(result) {
  console.log(result); // 'Another value'
}).catch(function(error) {
  console.error(error);
});

Deferreds

Deferreds provide a way to expose the resolve and reject functions of a promise outside of its constructor. This can be useful when the resolution of a promise needs to be controlled externally.

var deferred = Promise.defer();

deferred.promise.then(function(result) {
  console.log(result);
});

deferred.resolve('Deferred success!');

Promise.all

Promise.all is used to wait for all promises in an iterable to be resolved. The resulting promise is resolved with an array of the resolved values from the passed promises.

var p1 = Promise.resolve(3);
var p2 = 1337;
var p3 = new Promise(function (resolve, reject) {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([p1, p2, p3]).then(function(values) {
  console.log(values); // [3, 1337, 'foo']
});

Promise.race

Promise.race is used to wait only for the first promise to be resolved or rejected. It resolves or rejects with the value from that promise.

var p1 = new Promise(function(resolve, reject) { 
    setTimeout(resolve, 500, 'one'); 
});
var p2 = new Promise(function(resolve, reject) { 
    setTimeout(resolve, 100, 'two'); 
});

Promise.race([p1, p2]).then(function(value) {
  console.log(value); // 'two'
});

Other packages similar to promise

FAQs

Package last updated on 25 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc